package com.emip.job.method;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.emip.bean.AlterLogBean;
import com.emip.bean.ItemBean;
import com.emip.bean.MonthStoreBean;
import com.emip.bean.QuarterStoreBean;
import com.emip.bean.SiteBean;
import com.emip.bean.SiteItemBean;
import com.emip.bean.Store;
import com.emip.service.ItemService;
import com.emip.service.MonthStoreService;
import com.emip.service.QuarterStoreService;
import com.emip.service.SiteService;
import com.emip.util.report.Comput;
import com.emip.util.report.Statistics;


@Component
public class QuarterMethod {

	static Logger logger = Logger.getLogger(MonthMethod.class);
	
	@Autowired
	MonthStoreService monthStoreService;
	
	@Autowired
	QuarterStoreService quarterStoreService;
	
	@Autowired
	ItemService itemService;
	
	@Autowired
	SiteService siteService;
	
	public Store comput(Calendar calendar, Statistics statistic, String[] items, String siteId) {
		Calendar cal = (Calendar) calendar.clone();
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH);
		if (month == 0) {//当第一个月时，计算去年最后一个季度的值
			year--;
			month = 10;
		} else if (month%3 == 0) {//当第4、7、10月时，计算上一个季度值
			month = month - 3;
		} else {//计算当前季度值
			month = month/3*3 + 1;
		}
		List<MonthStoreBean> stores = monthStoreService.getByTimespan(year, month, year, month+2, items, siteId);
		logger.info("Report Job, Computing QUARTER Data:" + 
				year + "/" + month + "  --  " +
				year + "/" + month+2);
		
		statistic.init();
		statistic.comput(stores);
		Map<String, Comput> computs = statistic.getResults();
		QuarterStoreBean quarter = null;
		for (Map.Entry<String, Comput> entry: computs.entrySet()) {
			Comput comput = entry.getValue();
			String[] key = entry.getKey().split("_");
			if (!key[0].equals("AverageComput")) continue;
			quarter = quarterStoreService.findByQuarter(year+"", month/3+"", key[1], key[2]);
			if (quarter == null) {
				quarter = new QuarterStoreBean();
				quarter.setQuater((month-1)/3+1);
				quarter.setYear(year+"");

				SiteBean site = siteService.getSiteByID(key[1]);
				quarter.setSite(site);

				ItemBean item = itemService.getItemByID(key[2]);
				quarter.setItem(item);
				
				if (item == null || site == null) continue;
			}
			quarter.setFlag(comput.getFlag());
			String result = null;
			try {
				result = comput.getResult();
			} catch (Exception e) {
				continue;
			}
			if (result == null || comput.getCount() == 0) continue;
			quarter.setValue(result);
			quarterStoreService.merge(quarter);
		}
		return quarter;
	}
	
	public void doWork(Calendar clone, Statistics statistic) {
		List<SiteItemBean> siteItems = statistic.getSiteItems();
		for (int i = 0; i < siteItems.size(); i++) {
			SiteItemBean siteItem = siteItems.get(i);
			String itemId = siteItem.getItem().getId();
			String siteId = siteItem.getSite().getId();
			this.comput(clone, statistic, new String[]{itemId}, siteId);
		}
	}
	
	public void statistic(Calendar clone, Statistics statistic) {
		List<SiteItemBean> siteItems = statistic.getSiteItems();
		for (int i = 0; i < siteItems.size(); i++) {
			SiteItemBean siteItem = siteItems.get(i);
			String itemId = siteItem.getItem().getId();
			String siteId = siteItem.getSite().getId();
			comput(clone, statistic, new String[]{itemId}, siteId);
		}
	}
	
	public void merge(AlterLogBean log) {
		String siteId = log.getSite().getId();
		String itemId = log.getItem().getId();
		String date = log.getDate();
		String[] dates = date.split("年|季");
		QuarterStoreBean store = 
				quarterStoreService.findByQuarter(dates[0], dates[1], siteId, itemId);
		store.setValue(log.getNewvalue());
		quarterStoreService.merge(store);
	}
}
